package it.pecuswap.backend.entities.base;

import it.pecuswap.backend.HibernateConfiguration;
import it.pecuswap.backend.exceptions.ConfigurationException;

import javax.persistence.EntityManager;

import org.hibernate.Session;

public class HibernateUtils {
	
	private static HibernateUtils instance;
	private Session session;
	
	private HibernateUtils(){
	}
	
	public static HibernateUtils getInstance(){
		if (instance == null){
			return new HibernateUtils();
		} else {
			return instance;
		}
	}

	public Session getSession(boolean isSharedSession){
		try {
			if (session == null || !isSharedSession){
				EntityManager entityManager = HibernateConfiguration.getInstance().getEntityManager();
				Session session = entityManager.unwrap(Session.class);
				return session;
			} else{
				return session;
			} 
		} catch (ConfigurationException e) {
			//FIXME:
			throw new IllegalStateException();
		}
	}
	
}
